package com.hdyg.sixg.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.AddressBean;
import com.hdyg.sixg.mvp.Contrant.CAddress;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PAddress;
import com.hdyg.sixg.util.StringUtil;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 收货地址
 * @time 2019/4/17 16:10
 */
public class AddressActivity extends BaseActivity implements CAddress.IVAddress {

    public static final int ADDRESS_ADD = 0x00001;//添加收货地址 未添加地址
    public static final int ADDRESS_EDIT = 0x00002;//修改收货地址
    public static final int ADDRESS_SAVE = 0x00003;//保存收货地址

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.ll_name)
    LinearLayout llName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.et_area)
    TextView etArea;
    @BindView(R.id.ll_area)
    LinearLayout llArea;
    @BindView(R.id.et_addr)
    EditText etAddr;
    @BindView(R.id.ll_addr)
    LinearLayout llAddr;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private static final int REQUEST_CODE = 0x000004;
    private int intentCode;
    private CAddress.IPAddress mPresenter;
    private String name, phone, country, city, region, address;
    private CityPickerView mPicker = new CityPickerView();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address;
    }

    @Override
    protected void initView() {
        mPicker.init(mContext);
        tvTopTitle.setText(R.string.mine_address);
        onClickListener();
        intentCode = getIntent().getIntExtra(SpMsg.INTENT_CODE, ADDRESS_ADD);//默认添加收货地址
        if (intentCode == ADDRESS_ADD) {
            btSure.setText(R.string.address_add);
            setChangeUI(false);
        } else if (intentCode == ADDRESS_EDIT) {
            btSure.setText(R.string.address_edit);
            setChangeUI(true);
        } else if (intentCode == ADDRESS_SAVE) {
            btSure.setText(R.string.address_save);
            setChangeUI(true);
        }
    }

    /**
     * 是否显示
     *
     * @param flag true 显示  false 不显示
     */
    private void setChangeUI(boolean flag) {
        if (flag) {
            llName.setVisibility(View.VISIBLE);
            llPhone.setVisibility(View.VISIBLE);
            llArea.setVisibility(View.VISIBLE);
            llAddr.setVisibility(View.VISIBLE);
        } else {
            llName.setVisibility(View.INVISIBLE);
            llPhone.setVisibility(View.INVISIBLE);
            llArea.setVisibility(View.INVISIBLE);
            llAddr.setVisibility(View.INVISIBLE);
        }
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //选择省市区
        etArea.setOnClickListener(v -> {
            CityConfig cityConfig = new CityConfig.Builder().build();
            mPicker.setConfig(cityConfig);
            //监听选择点击事件及返回结果
            mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
                @Override
                public void onSelected(ProvinceBean province, CityBean cityBean, DistrictBean district) {
                    //省份province   城市city  地区district
                    country = province.getName();
                    city = cityBean.getName();
                    region = district.getName();
                    String str = String.format(getResources().getString(R.string.address_p_c), country, city, region);
                    etArea.setText(str);
                }

                @Override
                public void onCancel() {

                }
            });
            //显示
            mPicker.showCityPicker();
        });
        //保存/修改/添加
        btSure.setOnClickListener(v -> {
            if (intentCode == ADDRESS_ADD) {
                mBundle = new Bundle();
                mBundle.putInt(SpMsg.INTENT_CODE, ADDRESS_SAVE);
                startActivityForResult(AddressActivity.class, mBundle, REQUEST_CODE);
            } else if (intentCode == ADDRESS_EDIT) {
                //修改地址
                saveAddress();
            } else if (intentCode == ADDRESS_SAVE) {
                //添加地址
                saveAddress();
            }
        });
    }

    private void saveAddress() {
        name = etName.getText().toString().trim();
        phone = etPhone.getText().toString().trim();
        address = etAddr.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            toastNotifyShort(R.string.address_user_hint);
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            toastNotifyShort(R.string.address_phone_hint);
            return;
        }
        if (TextUtils.isEmpty(address)) {
            toastNotifyShort(R.string.address_addr_hint);
            return;
        }
        if (TextUtils.isEmpty(country)) {
            toastNotifyShort(R.string.address_area_hint);
            return;
        }
        if (StringUtil.isFastDoubleClick()){
            return;
        }
        mPresenter.pGetSaveAddress(BaseUrl.SAVE_ADDRESS_URL, GetParamUtil.getSaveAddressParam(name, phone, country, city, region, address));
    }

    @Override
    protected void initData() {
        if (intentCode != ADDRESS_SAVE){
            mPresenter.pGetAddress(BaseUrl.GET_ADDRESS_URL, GetParamUtil.getAddressParam());
        }
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PAddress(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CODE:
                initData();
                break;
        }
    }

    @Override
    public void vGetAddressSuccess(AddressBean dataBean) {
        if (dataBean.getHave_address() == 0) {
            //未绑定收货地址
            intentCode = ADDRESS_ADD;
            btSure.setText(R.string.address_add);
            setChangeUI(false);
        } else if (dataBean.getHave_address() == 1) {
            //已绑定收货地址
            intentCode = ADDRESS_EDIT;
            btSure.setText(R.string.address_edit);
            setChangeUI(true);
            etName.setText(dataBean.getAddress().getName());
            etPhone.setText(dataBean.getAddress().getPhone());
            country = dataBean.getAddress().getCountry();
            city = dataBean.getAddress().getCity();
            region = dataBean.getAddress().getRegion();
            String str = String.format(getResources().getString(R.string.address_p_c), country, city, region);
            etArea.setText(str);
            etAddr.setText(dataBean.getAddress().getAddress());
        }
    }

    @Override
    public void vGetSaveAddressSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }
}
